package kit.views;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import kit.core.Core;
import kit.core.ToHalfEntropyCalculator;
import kit.core.ToQuarterRootEntropyCalculator;
import kit.core.ToZeroEntropyCalculator;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class SenderReceiverPanel extends KitPanel {
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private final ButtonGroup formulaGroup = new ButtonGroup();
	private JComboBox failures;

	/**
	 * Create the panel.
	 */
	public SenderReceiverPanel() {
		setLayout(new BorderLayout(0, 0));

		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		add(panel, BorderLayout.SOUTH);

		JButton btnNewButton = new JButton("Salva");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Core core = Core.obtainSingleton();
				KitFrame frame = (KitFrame) SwingUtilities
						.getRoot(SenderReceiverPanel.this);
				if (SenderReceiverPanel.this.getSenderComboBox().getSelectedIndex() < 0
						|| SenderReceiverPanel.this.getReceiverComboBox().getSelectedIndex() < 0) {
					SenderReceiverPanel.this
							.showErrorDialog(
									"E' necessario selezionare mittente e destinatario per procedere.",
									frame);
				} else {
					String senderUID = (String) SenderReceiverPanel.this
							.getSenderComboBox().getSelectedItem();
					String receiverUID = (String) SenderReceiverPanel.this
							.getReceiverComboBox().getSelectedItem();
					String failures = (String) SenderReceiverPanel.this
							.getFailuresComboBox().getSelectedItem();
					core.setNewChallenge(Integer.parseInt(failures), senderUID,
							receiverUID);

					frame.getMnSegreti().setEnabled(true);
					frame.getMnRound().setEnabled(true);
					frame.getCenterPanel().removeAll();
					frame.repaint();

					int fChoice = formulaGroup.getSelection().getMnemonic();
					if (fChoice == 90) {
						core.setCalc(new ToZeroEntropyCalculator());
					} else if (fChoice == 81) {
						core.setCalc(new ToQuarterRootEntropyCalculator());
					} else {
						core.setCalc(new ToHalfEntropyCalculator());
					}
					frame.getStatusLabel().setText(core.getCurrentChallenge().toString());
				}
			}
		});
		btnNewButton.setHorizontalAlignment(SwingConstants.RIGHT);
		panel.add(btnNewButton);

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new FormLayout(
				new ColumnSpec[] { FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, }, new RowSpec[] {
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.DEFAULT_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("173px:grow"), }));

		JLabel lblNumeroMassimoDi = new JLabel("Numero massimo di fallimenti:");
		lblNumeroMassimoDi.setFont(new Font("Dialog", Font.PLAIN, 12));
		panel_1.add(lblNumeroMassimoDi, "2, 2, right, default");

		failures = new JComboBox();
		failures.setModel(new DefaultComboBoxModel(new String[] { "1", "2",
				"3", "4", "5", "6" }));
		failures.setSelectedIndex(2);
		panel_1.add(failures, "4, 2, fill, default");

		JLabel lblMittente = new JLabel("Mittente:");
		lblMittente.setFont(new Font("Dialog", Font.PLAIN, 12));
		panel_1.add(lblMittente, "2, 4, right, default");

		comboBox = new JComboBox();
		panel_1.add(comboBox, "4, 4, fill, default");

		JLabel lblDestinatario = new JLabel("Destinatario:");
		lblDestinatario.setFont(new Font("Dialog", Font.PLAIN, 12));
		panel_1.add(lblDestinatario, "2, 6, right, default");

		comboBox_1 = new JComboBox();
		panel_1.add(comboBox_1, "4, 6, fill, default");

		JTextArea txtrFormulePerLa = new JTextArea();
		txtrFormulePerLa.setBackground(UIManager.getColor("Button.background"));
		txtrFormulePerLa.setEditable(false);
		txtrFormulePerLa.setText("Correzione dell'entropia:");
		panel_1.add(txtrFormulePerLa, "2, 8, fill, fill");

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, "4, 8, fill, fill");
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));

		JRadioButton rdbtnZero = new JRadioButton("E' = 0");
		rdbtnZero.setMnemonic('z');
		panel_3.add(rdbtnZero);
		rdbtnZero.setFont(new Font("Dialog", Font.PLAIN, 12));
		formulaGroup.add(rdbtnZero);
		rdbtnZero.setSelected(true);

		JRadioButton rdbtnHalf = new JRadioButton("E' = E * 1/2");
		rdbtnHalf.setMnemonic('h');
		panel_3.add(rdbtnHalf);
		rdbtnHalf.setFont(new Font("Dialog", Font.PLAIN, 12));
		formulaGroup.add(rdbtnHalf);

		JRadioButton rdbtnQuarter = new JRadioButton("E' = E * 1/4");
		rdbtnQuarter.setMnemonic('q');
		panel_3.add(rdbtnQuarter);
		rdbtnQuarter.setFont(new Font("Dialog", Font.PLAIN, 12));
		formulaGroup.add(rdbtnQuarter);

		JPanel panel_2 = new JPanel();
		add(panel_2, BorderLayout.NORTH);
		panel_2.setLayout(new BorderLayout(0, 0));

	}

	protected JComboBox getSenderComboBox() {
		return comboBox;
	}

	public JComboBox getReceiverComboBox() {
		return comboBox_1;
	}

	protected ButtonGroup getFormulaGroup() {
		return formulaGroup;
	}

	public JComboBox getFailuresComboBox() {
		return failures;
	}
}
